#include "SW_DP.h"
#include "DAP.h"
#include "dap_setting.h"
#include "SW_DP_SPI.h"

void PORT_SWD_SETUP(void)
{
    pMdLink_DAP_Setting_t dap_setting = get_dap_setting();
    PORT_Mode_t mode = dap_setting->swd_port_mode;
    switch (mode) {
        case PORT_MODE_GPIO:
            IO_PORT_SWD_SETUP();
            break;
        case PORT_MODE_DEDIC_GPIO:
            Dedic_IO_PORT_SWD_SETUP();
            break;
        case PORT_MODE_SPI:
            SPI_PORT_SWD_SETUP();
        default:
            return;
    }
}

/**
 * @brief 通过SWJ接口发送特定序列（支持JTAG和SWD模式）
 * @param count 要发送的位数
 * @param data 要发送的数据指针
 * @note 根据当前调试端口类型选择不同的发送方式：
 *       - JTAG模式：临时切换为GPIO控制，发送后恢复
 *       - SWD模式：根据设置选择SPI或GPIO方式发送
 */
void SWJ_Sequence(uint32_t count, const uint8_t *data)
{
    pMdLink_DAP_Setting_t dap_setting = get_dap_setting();
    PORT_Mode_t mode = dap_setting->swd_port_mode;
    switch (mode) {
        case PORT_MODE_GPIO:
            IO_SWJ_Sequence(count, data);
            break;
        case PORT_MODE_DEDIC_GPIO:
            Dedic_IO_SWJ_Sequence(count, data);
            break;
        case PORT_MODE_SPI:
            SPI_SWJ_Sequence(count, data);
        default:
            return;
    }
}

void SWD_Sequence(uint32_t info, const uint8_t *swdo, uint8_t *swdi)
{
    pMdLink_DAP_Setting_t dap_setting = get_dap_setting();
    PORT_Mode_t mode = dap_setting->swd_port_mode;
    switch (mode) {
        case PORT_MODE_GPIO:
            IO_SWD_Sequence(info, swdo, swdi);
            break;
        case PORT_MODE_DEDIC_GPIO:
            return Dedic_IO_SWD_Sequence(info, swdo, swdi);
            break;
        case PORT_MODE_SPI:
            SPI_SWD_Sequence(info, swdo, swdi);
            break;
        default:
            break;
    }
}
#include "esp_log.h"
uint8_t SWD_Transfer(uint32_t request, uint32_t *data)
{
    pMdLink_DAP_Setting_t dap_setting = get_dap_setting();
    PORT_Mode_t mode = dap_setting->swd_port_mode;
    switch (mode) {
        case PORT_MODE_GPIO:
            return IO_SWD_Transfer(request, data);
        case PORT_MODE_DEDIC_GPIO:
            return Dedic_IO_SWD_Transfer(request, data);


        case PORT_MODE_SPI:
             return SPI_SWD_Transfer(request, data);
        default:
            return 0;
    }
}
